package com.practices.db;

import java.util.ArrayList;
import java.util.List;

import com.practices.bean.DownloadInfo;

import android.R.color;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {
	private static final String DBNAME = "download.db";
	private static final String TABLENAME = "download_info";

	private static final String COLUMN_THREAD_ID = "thread_id";
	private static final String COLUMN_START_POS = "start_pos";
	private static final String COLUMN_END_POS = "end_pos";
	private static final String COLUMN_COMPELETE_SIZE = "compelete_size";
	private static final String COLUMN_URL = "url";

	private static final int VERSION = 1;

	public DBOpenHelper(Context context) {
		super(context, DBNAME, null, 1);
	}

	public static DBOpenHelper getDBOpenHelper(Context cx) {
		return new DBOpenHelper(cx);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// id , thread_id ,start_pos,end_pos,comelete_size, url
		db.execSQL("create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, thread_id integer, " + "start_pos integer, end_pos integer, compelete_size integer,url char)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// db.execSQL("DROP TABLE IF EXISTS download_info");
		// onCreate(db);
	}

	public void addOneDownloadInfo(Context cx, int thread_id, int start_pos, int end_pos, int compelete_size, String url) {
		SQLiteDatabase db = DBOpenHelper.getDBOpenHelper(cx).getWritableDatabase();
		String sql = " insert into " + TABLENAME + " (thread_id,start_pos, end_pos,compelete_size,url) values (?,?,?,?,?) ";
		Object[] bindArgs = { thread_id, start_pos, end_pos, compelete_size, url };
		db.execSQL(sql, bindArgs);
		db.close();
	}

	// 通过URL取出单挑信息
	public List<DownloadInfo> queryDownloadInfo(Context cx, String url) {
		SQLiteDatabase db = DBOpenHelper.getDBOpenHelper(cx).getReadableDatabase();
		String sql = "select thread_id,start_pos,end_pos,compelete_size from " + TABLENAME + " where url=?";
		Object[] bindArgs = { url };
		Cursor cursor = db.rawQuery(sql, new String[] { url });
		List<DownloadInfo> list = new ArrayList<DownloadInfo>();
		DownloadInfo downloadInfo = null;
		while (cursor.moveToNext()) {
			downloadInfo.setThreadId(cursor.getInt(cursor.getColumnIndex("thread_id")));
			downloadInfo.setStartPos(cursor.getInt(cursor.getColumnIndex("start_pos")));
			downloadInfo.setEndPos(cursor.getInt(cursor.getColumnIndex(this.COLUMN_END_POS)));
			downloadInfo.setCompleteSize(cursor.getInt(cursor.getColumnIndex(COLUMN_COMPELETE_SIZE)));
			downloadInfo.setUrl(cursor.getString(cursor.getColumnIndex(COLUMN_URL)));
			list.add(downloadInfo);
		}
		cursor.close();
		db.close();
		return list;
	}
	/**
	 * 
	* @Title: deleteOneDownloadInfo
	* @Description: 
	* @param @param cx
	* @param @param url    
	* @return void    返回类型
	* @throws
	 */
	public void deleteOneDownloadInfo(Context cx, String url) {
		SQLiteDatabase db = DBOpenHelper.getDBOpenHelper(cx).getWritableDatabase();
		String sql = "delete from " + TABLENAME + "where url= ? ";
		Object[] bindArgs = { "url" };
		db.execSQL(sql, bindArgs);
		db.close();
	}
	
	
	
	/**
	* @Title: updataInfos
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param cx
	* @param @param threadId
	* @param @param compeleteSize
	* @param @param urlstr    
	* @return void    返回类型
	* @throws
	*/
	public void updataInfos(Context cx, int threadId, int compeleteSize, String urlstr) {
		SQLiteDatabase database = DBOpenHelper.getDBOpenHelper(cx).getWritableDatabase();
		String sql = "update download_info set compelete_size=? where thread_id=? and url=?";
		Object[] bindArgs = { compeleteSize, threadId, urlstr };
		database.execSQL(sql, bindArgs);
		database.close();
	}

	/**
	 * 下载完成后删除数据库中的数据
	 */
	public void delete(Context cx, String url) {
		SQLiteDatabase database = DBOpenHelper.getDBOpenHelper(cx).getReadableDatabase();
		database.delete("download_info", "url=?", new String[] { url });
		database.close();
	}
	
	/**
	 * 查看数据库中是否有数据
	 */
	public boolean isHasInfors(Context cx,String urlstr) {
		SQLiteDatabase database = DBOpenHelper.getDBOpenHelper(cx).getReadableDatabase();
		String sql = "select count(*)  from download_info where url=?";
		Cursor cursor = database.rawQuery(sql, new String[] { urlstr });
		cursor.moveToFirst();
		int count = cursor.getInt(0);
		cursor.close();
		return count == 0;
	}

}
